﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Defect_Tracking_System.Models;
using Defect_Tracking_System.Controllers.ControllerExtensions;


namespace Defect_Tracking_System.Controllers
{
    public class ReportController : Controller
    {
		
        [Authorize]
        public ActionResult OnLookups()
        {
            return View();
        }

        [Authorize, HttpPost]
        public ActionResult OnLookups(String TypeReport, String ProjectId)
        {
            if (TypeReport == "DefectStatus" || TypeReport == "DefectPriority" || TypeReport == "ProjectVersion" || TypeReport == "DefectType")
            {
                List<PieChartItem> Items = ReportManagement.GetInstance().GetPieChartProjectReport(TypeReport, ProjectId);
                return Json(Items);
            }
            return View();
        }

        [Authorize]
        public ActionResult OnWeek()
        {
            return View();
        }

        [Authorize]
        public ActionResult OnMonth()
        {
            return View();
        }

        [Authorize]
        public ActionResult OnYear()
        {
            return View();
        }

        [Authorize]
        public ActionResult CustomDate()
        {
            return View();
        }

        [Authorize, HttpPost]
        public ActionResult ProjectReportWithDate(String TypeReport, String ProjectId)
        {
            DateTime From = DateTime.Now;
            DateTime To = DateTime.Now;

            if (TypeReport == "DefectOnThisWeek")
            {
                From = DateTime.Now.StartOfWeek(DayOfWeek.Monday);
                To = From.AddDays(7);

                List<LineChartItem> Chart = ReportManagement.GetInstance().GetLineChartProjectReport(From, To, ProjectId);

                return Json(Chart); 
            }
            else if(TypeReport == "DefectOnThisMonth")
            {
                From = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

                To = From.AddMonths(1).AddDays(-1);

                List<LineChartItem> Chart = ReportManagement.GetInstance().GetLineChartProjectReport(From, To, ProjectId);

                return Json(Chart); 
            }
            else if (TypeReport == "DefectOnThisYear")
            {
                From = new DateTime(DateTime.Now.Year, 1, 1);
                To = new DateTime(DateTime.Now.Year, 12, 31);

                MasterChartModel Model = ReportManagement.GetInstance().GetMasterCharReport(From, To, ProjectId);

                return Json(Model);
            }

            return View();
        }

        [Authorize(Roles="Project Admin, Group Admin")]
        public ActionResult OnGroup()
        {
            return View();
        }

        [Authorize(Roles = "Project Admin, Group Admin"), HttpPost]
        public ActionResult OnGroup(String TypeReport)
        {
            return View();
        }

        //public ActionResult ExportToExcel()
        //{
        //    string searchContent = "";

        //    if (Request.QueryString["searchContent"] != null)
        //    {
        //        searchContent = Request.QueryString["searchContent"];
        //    }

        //    var customers = customerRepository.FindAllCustomer(searchContent);

        //    string[] headers = new string[9] 
        //    { 
        //        "CustomerID",
        //        "FirstName",
        //        "MiddleName",
        //        "LastName",
        //        "Email",
        //        "Address",
        //        "PhoneNumber",
        //        "Decription",
        //        "ReceiveNewsletter"
        //    };

        //    return this.Excel("data.xlsx", "Customers", customers, headers, headers);
        //}

    }

    public static class DateTimeExtensions
    {
        public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
        {
            int diff = dt.DayOfWeek - startOfWeek;
            if (diff < 0)
            {
                diff += 7;
            }

            return dt.AddDays(-1 * diff).Date;
        }
    }   
}